function res=psvar(VARinnovations,modelSpec)
%identification using external instruments (Stock&Watson 2012, Mertens&Ravn 2013)

n  =size(VARinnovations,2);
nL =modelSpec.nVARlags;

instrumentSet=modelSpec.instrument;

%find relevant proxy variable
select =ismember(instrumentSet.labels,modelSpec.selectedInstrument);
proxy  =instrumentSet.data(:,select);


%match sample
residdates =modelSpec.dataStructure.dates(1+nL:end);
proxydates =instrumentSet.dates;

lowerT=max(residdates(1),proxydates(1));
upperT=min(residdates(end),proxydates(end));


innovations =VARinnovations(residdates >= lowerT & residdates <=upperT ,:)';
proxy       =proxy(proxydates >= lowerT & proxydates <=upperT );


keepRows    =~isnan(proxy);
proxy       =proxy(keepRows,:);
innovations =innovations(:,keepRows);


%identification
PSVAR=ProxySVARidentification(innovations,find(modelSpec.shockVar),proxy);

%Bzero
Bzero=zeros(n,n); Bzero(:,modelSpec.shockVar)=PSVAR.B/PSVAR.B(modelSpec.shockVar);


%load structure
res.Bzero       =Bzero;
res.Reliability =PSVAR.L;
res.Fstat       =PSVAR.fstat;
end
